package com.starwish.starwishblog.advice;

import com.starwish.starwishblog.entity.User;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.UserService;
import com.starwish.starwishblog.vo.AccountVo;
import com.starwish.starwishblog.vo.UserLoginVo;
import com.starwish.starwishblog.vo.UserOperationVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

@Component    //把这个类交给 容器实例化
@Aspect      //表示这个类是一个 切面类
public class UserAdvice<T> {
    private String username;
    private Integer userId;
    private String operationName;
    @Autowired
    UserService userService;

    @Pointcut("execution(* com.starwish.starwishblog.web.front.FUserController.login(..))")
    public void initInfo(){

    }

    @Before("initInfo()")
    public void initInfo(JoinPoint joinPoint){
        AccountVo accountVo = (AccountVo) joinPoint.getArgs()[0];
        username = accountVo.getAdminName();
        User user = userService.login(username);
        if(user !=null){
            userId = user.getId();
        }
    }
    @AfterReturning(value = "execution(* com.starwish.starwishblog.web.front.*.*(..))",returning = "methodResult")
    public void afterReturning(JoinPoint joinPoint,Object methodResult){
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        Method method = ms.getMethod();
        String className1 = joinPoint.getTarget().getClass().toString();
        ResultResponse<T> resultResponse = (ResultResponse<T>) methodResult;
        operationName = className1.substring(className1.lastIndexOf(".")+1)+"==>"+method.getName();

        System.out.println(className1.substring(className1.lastIndexOf(".")+1)+method.getName());
        if("FUserController==>login".equals(operationName)){
            UserLoginVo loginVo = new UserLoginVo();
            loginVo.setUserId(userId);
            loginVo.setUsername(username);
            loginVo.setCreateTime(new Date());
            loginVo.setOperationType(operationName);
            loginVo.setResult(resultResponse.getMsg());
            loginVo.setIsDelete("0");
            int i =userService.addUserLoginLog(loginVo);
        }else if("FUserController==>loginOut".equals(operationName)){
            UserLoginVo loginVo = new UserLoginVo();
            loginVo.setUserId(userId);
            loginVo.setUsername(username);
            loginVo.setCreateTime(new Date());
            loginVo.setOperationType(operationName);
            loginVo.setResult(resultResponse.getMsg());
            loginVo.setIsDelete("0");
            int i =userService.addUserLoginLog(loginVo);
        }else if("FUserController==>register".equals(operationName)){
            //注册记录
            AccountVo accountVo = (AccountVo) joinPoint.getArgs()[0];
            username = accountVo.getAdminName();
            UserOperationVo operationVo = new UserOperationVo();
            operationVo.setUsername(username);
            operationVo.setCreateTime(new Date());
            operationVo.setOperationType(operationName);
            operationVo.setResult(resultResponse.getMsg());
            int i = userService.addUserOperationLog(operationVo);
        }else {
            UserOperationVo operationVo = new UserOperationVo();
            operationVo.setUseId(userId);
            operationVo.setUsername(username);
            operationVo.setCreateTime(new Date());
            operationVo.setOperationType(operationName);
            operationVo.setResult(resultResponse.getMsg());
            int i = userService.addUserOperationLog(operationVo);
        }
    }

}
